﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using University.Workshop.Model;

namespace University.Workshop.DataAccess
{
    public class DataAccessOtherCollectControl
    {
        public static ICollection<Model.OtherCollectControl> GetBetweenDates(DateTime initalDate, DateTime endDate)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from c in WFPEntities.Instance.OtherCollectControls
                    where c.CollectPayDate >= initalDate && c.CollectPayDate <= endDate
                    select c).ToList();
        }

        public static ICollection<Model.OtherCollectControl> SearchByClientId(Model.CollectControl chargetoFind)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from c in WFPEntities.Instance.OtherCollectControls
                    where c.KardexId == chargetoFind.KardexId && c.IsPaid == false
                    select c).ToList();
        }

        public static void UpdateCollect(Model.CollectControl collectToUpdate)
        {
            WFPEntities.Instance.DiscardChanges();

            Model.OtherCollectControl cc = (from wp in WFPEntities.Instance.OtherCollectControls
                                            where wp.CollectId == collectToUpdate.CollectId
                                            select wp).First();

            cc.CollectPayDate = collectToUpdate.CollectPayDate;
            cc.IsPaid = true;

            WFPEntities.Instance.SaveChanges();
        }

        public static void RegisterOtherCollectControl(Model.OtherCollectControl otherCollect)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.OtherCollectControls.Add(otherCollect);
            WFPEntities.Instance.SaveChanges();
        }

        public static bool ExistAnyOtherCollectWithThisKardexIDAndDate(int kardexID, DateTime date)
        {
            var collects = (from oc in WFPEntities.Instance.OtherCollectControls
                            where oc.KardexId == kardexID && oc.DateGenerated.Month == date.Month && oc.DateGenerated.Year == date.Year && oc.CollectType == (short)OtherChargeType.Aporte
                            select oc).ToList ();

            return (collects.Count > 0 ? true : false);
        }

    }
}
